package day12.handler.simpleChannelHandler.request;

import day12.packet.request.LoginRequestPacket;
import day12.packet.response.LoginResponsePacket;
import day12.util.IDUtil;
import day12.util.LoginUtil;
import day12.util.session.Session;
import day12.util.session.SessionUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author chenshijie
 * @date 2023/12/26 13:13
 */
@ChannelHandler.Sharable
public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginRequestPacket> {

    public static final LoginRequestHandler INSTANCE = new LoginRequestHandler();

    protected LoginRequestHandler() {

    }
    private final static String USERNAME = "yu";
    private final static String PASSWORD = "password";

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginRequestPacket loginRequestPacket) throws Exception {
        login(ctx, loginRequestPacket);
    }

    private static boolean valid(LoginRequestPacket loginRequestPacket) {
        return true;
//        return USERNAME.equals(loginRequestPacket.getUsername()) && PASSWORD.equals(loginRequestPacket.getPassword());
    }

    private static void login(ChannelHandlerContext ctx, LoginRequestPacket loginRequestPacket) {
        LoginResponsePacket loginResponsePacket = new LoginResponsePacket();
        loginResponsePacket.setVersion(loginRequestPacket.getVersion());
        if (loginRequestPacket.getLoginStatus() == LoginUtil.LOGOUT) {
            Session session = SessionUtil.getSession(ctx.channel());
            System.out.println(ctx.channel());
            System.out.println("用户Id为 【" + session.getUserId() + "】, 用户名为 【" + session.getUserName() + "】 退出登录");
            SessionUtil.unBindSession(ctx.channel());
        } else {
            if (valid(loginRequestPacket)) {
                // 成功

                int userId = IDUtil.randomNumber();
                loginResponsePacket.setUserId(userId);
                loginResponsePacket.setSuccess();
                loginResponsePacket.setMessage("登录成功");;
                loginResponsePacket.setUsername(loginRequestPacket.getUsername());
//                System.out.println(ctx.channel());
                System.out.println("用户id为 【" + userId + "】, 用户名为 【" + loginRequestPacket.getUsername() + "】 登录成功");
//            LoginUtil.markAsLogin(ctx.channel());
                SessionUtil.bindSession(new Session(userId, loginRequestPacket.getUsername()), ctx.channel());
            } else {
                // 错误
                System.out.println("客户端校验失败");
                loginResponsePacket.setFail();
                loginResponsePacket.setMessage("密码错误或者账号不存在");
            }
            ctx.channel().writeAndFlush(loginResponsePacket);
        }

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Session session = SessionUtil.getSession(ctx.channel());
        System.out.println(ctx.channel());
        System.out.println("用户Id为：" + session.getUserId() + ",用户名为【" + session.getUserName() + "】退出登录");
        SessionUtil.unBindSession(ctx.channel());

    }
}
